package com.huarui.part.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;

import com.huarui.params.Params;
import com.huarui.part.model.CarModel;
import com.huarui.part.service.PartService;
import com.huarui.util.PinyinToolkit;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class CarModelAction extends BaseAction {

	private static final long serialVersionUID = 2924526709280705961L;

	private CarModel carModel;

	private PartService partService;

	private List<CarModel> carModelList;
	
	private Map<String, Object> mesMap;
	
	private String queryParam;
	
	private Integer start;

	public Map<String, Object> getMesMap() {
		return mesMap;
	}

	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}

	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public CarModel getCarModel() {
		return carModel;
	}

	public void setCarModel(CarModel carModel) {
		this.carModel = carModel;
	}

	public String toAdd() {

		return "input";
	}

	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "carModel.brand", key = "carModel.brand.required"),
			@RequiredStringValidator(fieldName = "carModel.name", key = "carModel.name.required"),
			@RequiredStringValidator(fieldName = "carModel.manuFactory", key = "carModel.manuFactory.required"),
			@RequiredStringValidator(fieldName = "carModel.shortName", key = "carModel.shortName.required") })
	public String doAdd() {

		carModel.setBrand(carModel.getBrand().trim());
		carModel.setManuFactory(carModel.getManuFactory().trim());
		carModel.setName(carModel.getName().trim());
		carModel.setShortName(carModel.getShortName().trim());
		carModel.setPyCode(PinyinToolkit.cn2FirstSpell(carModel.getName()));
		partService.createCarModel(carModel);

		return SUCCESS;
	}

	public String toSerach() {
		
		return "toSerach";
	}

	public String doSerach() {

		carModelList = partService.getCarModels(carModel.getName());
		return "toSerach";
	}

	public String toList() {

		carModelList = partService.getCarModels(null);

		return "toList";
	}

	public String toModi() {
		carModel = partService.getCarModelById(carModel.getId());
		return "toModi";
	}

	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "carModel.brand", key = "carModel.brand.required"),
			@RequiredStringValidator(fieldName = "carModel.name", key = "carModel.name.required"),
			@RequiredStringValidator(fieldName = "carModel.manuFactory", key = "carModel.manuFactory.required"),
			@RequiredStringValidator(fieldName = "carModel.shortName", key = "carModel.shortName.required") })
	public String doModi() {

		carModel.setBrand(carModel.getBrand().trim());
		carModel.setManuFactory(carModel.getManuFactory().trim());
		carModel.setName(carModel.getName().trim());
		carModel.setShortName(carModel.getShortName().trim());
		carModel.setPyCode(PinyinToolkit.cn2FirstSpell(carModel.getName()));
		partService.updateCarModel(carModel);
		return SUCCESS;
	}

	public void setPartService(PartService partService) {
		this.partService = partService;
	}

	public List<CarModel> getCarModelList() {
		return carModelList;
	}

	public void setCarModelList(List<CarModel> carModelList) {
		this.carModelList = carModelList;
	}

	/**
	 * 后台查询
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listCarModel() {
		Map<String, Object> paramsMap = null;
		if(queryParam == null) {
			paramsMap = new HashMap<String, Object>();
		} else {
			JSONObject jo = JSONObject.fromObject(queryParam);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		}
		paramsMap.put("start", start == null ? 0 : start);
		paramsMap.put("pageSize", Params.BACKGROUND_PAGE_SIZE);
		mesMap = this.partService.getCarModelListByComplex(paramsMap);
		return SUCCESS;
	}
}
